﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Common.Helper;
using Common.Constants;
using Entities;
[assembly: CLSCompliant(true)]
namespace DataAccess
{
    /// <summary>
    /// contact dataaccess
    /// </summary>
    public class ContactDataAccess : IContactDataAccess
    {

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static ContactDataAccess Instance { get; private set; }
        /// <summary>
        /// Initializes the <see cref="ContactDataAccess" /> class.
        /// </summary>
        static ContactDataAccess() { Instance = new ContactDataAccess(); }



        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static Contact ExtractRawData(IDataRecord dataReader)
        {
            var contact = new Contact();
            int bestContactType;
            if (int.TryParse(dataReader["BestContactMethod"].ToString(), out bestContactType))
                contact.BestContactType = bestContactType;
            contact.ContactId = (Guid)dataReader["ContactID"];
            int contactType;
            if (int.TryParse(dataReader["ContactType"].ToString(), out contactType))

                contact.ContactType = contactType;
            if (dataReader["ManagerId"] != null && dataReader["ManagerId"].ToString() != String.Empty)
                contact.ManagerId = (Guid)dataReader["ManagerId"];

            contact.Email = dataReader["EmailAddress"].ToString();
            contact.FirstName = dataReader["FirstName"].ToString();
            bool isActive;
            if (bool.TryParse(dataReader["IsActive"].ToString(), out isActive))
                contact.IsActive = isActive;
            contact.JobRole = dataReader["JobRole"].ToString();
            contact.JobTitle = dataReader["JobTitle"].ToString();
            contact.KnownAs = dataReader["KnownAs"].ToString();
            contact.MobilePhone = dataReader["MobilePhone"].ToString();
            contact.OfficePhone = dataReader["OfficePhone"].ToString();
            contact.StHomePhone = dataReader["STHomePhone"].ToString();
            contact.SurName = dataReader["SurName"].ToString();
            contact.Workbase = dataReader["Workbase"].ToString();
            return contact;


        }
        /// <summary>
        /// Inserts the specified contact.
        /// </summary>
        /// <param name="contact">The contact.</param>
        /// <returns></returns>
        public int Insert(Contact contact)
        {
            var colParameter = new Collection<SqlParameter>
                    {
                        new SqlParameter("BestContactMethod",contact.BestContactType),
                        new SqlParameter("ContactId",contact.ContactId),
                        new SqlParameter("ContactType",contact.ContactType),
                        new SqlParameter("EmailAddress",contact.Email),
                         new SqlParameter("FirstName",contact.FirstName),
                        new SqlParameter("IsActive",contact.IsActive),
                        new SqlParameter("JobRole",contact.JobRole),
                        new SqlParameter("JobTitle",contact.JobTitle),
                         new SqlParameter("KnownAs",contact.KnownAs),

                        new SqlParameter("ManagerId",contact.ManagerId),
                        new SqlParameter("MobilePhone",contact.MobilePhone),
                        new SqlParameter("StHomePhone",contact.StHomePhone),
                         new SqlParameter("OfficePhone",contact.OfficePhone),
                        new SqlParameter("SurName",contact.SurName),
                        new SqlParameter("Workbase",contact.Workbase)

                    };

            var resuft = SqlHelper.ExecuteNonQuery(ProcedureStores.InsertContact, colParameter);
            return resuft;
        }

        /// <summary>
        /// Updates the specified contact.
        /// </summary>
        /// <param name="contact">The contact.</param>
        /// <returns></returns>
        public int Update(Contact contact)
        {
            var colParameter = new Collection<SqlParameter>
                    {
                        new SqlParameter("BestContactMethod",contact.BestContactType),
                        new SqlParameter("ContactId",contact.ContactId),
                        new SqlParameter("ContactType",contact.ContactType),
                        new SqlParameter("EmailAddress",contact.Email),
                         new SqlParameter("FirstName",contact.FirstName),
                        new SqlParameter("IsActive",contact.IsActive),
                        new SqlParameter("JobRole",contact.JobRole),
                        new SqlParameter("JobTitle",contact.JobTitle),
                         new SqlParameter("KnownAs",contact.KnownAs),
                        new SqlParameter("ManagerId",contact.ManagerId),
                        new SqlParameter("MobilePhone",contact.MobilePhone),
                        new SqlParameter("StHomePhone",contact.StHomePhone),
                         new SqlParameter("OfficePhone",contact.OfficePhone),
                        new SqlParameter("SurName",contact.SurName),
                        new SqlParameter("Workbase",contact.Workbase)

                    };

            var resuft = SqlHelper.ExecuteNonQuery(ProcedureStores.UpdateContact, colParameter);
            return resuft;
        }

        /// <summary>
        /// Deletes the specified contact id.
        /// </summary>
        /// <param name="contactId">The contact id.</param>
        /// <returns></returns>
        public int Delete(string contactId)
        {
            var colParameter = new Collection<SqlParameter>
                    {
                        
                        new SqlParameter("ContactId",contactId)
                        
                    };

            var resuft = SqlHelper.ExecuteNonQuery(ProcedureStores.DeleteContact, colParameter);
            return resuft;
        }

        /// <summary>
        /// Gets the contact.
        /// </summary>
        /// <param name="contactId">The contact id.</param>
        /// <returns></returns>
        public Contact GetContact(string contactId)
        {
            var colParameter = new Collection<SqlParameter> { new SqlParameter("contactId", contactId) };

            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetContact, colParameter))
            {
                if (reader.Read())
                {
                    var result = ExtractRawData(reader);

                    return result;
                }
                return null;
            }
        }

        /// <summary>
        /// Lists the contact.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="surName">Name of the sur.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <returns></returns>
        public Collection<Contact> ListContact(string firstName, string surName,bool isActive)
        {
            var lstContact = new Collection<Contact>();

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("firstName", firstName),
                                       new SqlParameter("surName",surName),
                                       new SqlParameter("IsActive",isActive)
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.ListContact, colParameter))
            {
                while (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    lstContact.Add(result);
                }
                return lstContact;
            }
        }
        
        
    }
}
